using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DataAccess;
using Entity; 

namespace Business
{
    public class bProduct
    {
        public static int insertProduct(eProduct product)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@producName", SqlDbType.NVarChar);
            pr.Value = product.producName;
            paramList.Add(pr);

            pr = new SqlParameter("@producSecurityDeposit", SqlDbType.Float);
            pr.Value = product.producSecurityDeposit;
            paramList.Add(pr);

            pr = new SqlParameter("@producDescription", SqlDbType.NVarChar);
            pr.Value = product.producSecurityDeposit;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("insertProduct", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return r;
        }

        public static int updateProduct(eProduct product)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@productID", SqlDbType.Int);
            pr.Value = product.productID;
            paramList.Add(pr);

            pr = new SqlParameter("@producName", SqlDbType.NVarChar);
            pr.Value = product.producName;
            paramList.Add(pr);

            pr = new SqlParameter("@producSecurityDeposit", SqlDbType.Float);
            pr.Value = product.producSecurityDeposit;
            paramList.Add(pr);

            pr = new SqlParameter("@producDescription", SqlDbType.NVarChar);
            pr.Value = product.producSecurityDeposit;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("updateProduct", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return r;
        }

        public static int deleteProduct(int productID)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@productID", SqlDbType.Int);
            pr.Value = productID;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("deleteProduct", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return r;
        }

        public static DataTable getAllProduct()
        {
            DBHelper dbh = new DBHelper();
            DataTable dt = dbh.ExecuteQueryDataTable("getAllProduct", CommandType.StoredProcedure, new List<SqlParameter>());
            dbh.CloseConnection();
            return dt;
        }

        public static eProduct getProductByID(int productID)
        {
            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@productID", SqlDbType.Int);
            pr.Value = productID;
            paramList.Add(pr);

            IList il = bBase.LoadQueryString(typeof(eProduct), "getProductByID", CommandType.StoredProcedure, paramList);
            if (il.Count > 0)
            {
                return (eProduct)il[0];
            }
            else
            {
                return null;
            }
        }
        public static eProduct getProductByName(String productName)
        {
            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@producName", SqlDbType.NVarChar);
            pr.Value = productName;
            paramList.Add(pr);

            IList il = bBase.LoadQueryString(typeof(eProduct), "getProductByName", CommandType.StoredProcedure, paramList);
            if (il.Count > 0)
            {
                return (eProduct)il[0];
            }
            else
            {
                return null;
            }
        }
    }
}
